package com.jz.community.modulemine.information.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.CommUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.TimeUtils;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.commview.view.widget.LoginButton;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.information.task.UpdateUserInfoTask;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Tony.Fan on 2018/3/22 09:33
 */
public class SetResetPwdActivity extends BaseMvpActivity {
    @BindView(R2.id.login_reset_pwd_et)
    EditText loginResetPwdEt;
    @BindView(R2.id.login_confirm_reset_pwd_et)
    EditText loginConfirmResetPwdEt;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R2.id.reset_btn)
    LoginButton reset_btn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        setImmersionBar(titleToolbar);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_mine_reset_pwd_layout;
    }

    @Override
    protected void initDatas() {
        initTitle("设置密码", "");
        reset_btn.setText("确定");
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }


    @Override
    protected void addListener() {
        super.addListener();
        reset_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmResetClick(v);
            }
        });
    }

    //重置密码
    public void confirmResetClick(View view) {
        CommUtils.hideKeyboard(view, this);
        String password = loginResetPwdEt.getText().toString().trim();
        String password_again = loginConfirmResetPwdEt.getText().toString().trim();
        if (isHasPwdEmpty(SetResetPwdActivity.this, password, password_again)) {
            setUserInitPwd();
        }
    }

    /**
     * 设置初始化密码
     */
    private void setUserInitPwd() {
        new UpdateUserInfoTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                UserInfo userInfo = (UserInfo) o;
                if (!Preconditions.isNullOrEmpty(userInfo.getName())) {
                    WpToast.l(SetResetPwdActivity.this, "设置成功");
                    setResult(RESULT_OK);
                    finish();
                } else {
                    WpToast.l(SetResetPwdActivity.this, "设置失败");
                    refreshRegainBackground();
                }

            }
        }, true).execute(loginResetPwdEt.getText().toString().trim(), "3");
    }

    /**
     * 密码验证
     *
     * @param context
     * @param pwd
     * @param pwd1
     * @return
     */
    public static boolean isHasPwdEmpty(Context context, String pwd, String pwd1) {
        if (Preconditions.isNullOrEmpty(pwd) || pwd.length() < 6) {
            WpToast.m(context, R.string.password_empty);
            return false;
        }
        if (Preconditions.isNullOrEmpty(pwd1) || !pwd.equals(pwd1)) {
            WpToast.m(context, R.string.different_password);
            return false;
        }
        return true;
    }


    /**
     * 刷新按钮
     */
    private void refreshRegainBackground() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                reset_btn.regainBackground("确定");
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
